﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;

namespace Einstein.Group.DAL
{
    public class DeviceInfoDAL
    {
        public IList<DeviceInfo> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.DeviceInfo.ToList<DeviceInfo>();
            }
        }

        public DeviceInfo Selete(string deviceNO)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.DeviceInfo.Where(c => c.DeviceNO == deviceNO).FirstOrDefault();
            }
        }


        public bool Add(DeviceInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.DeviceInfo.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }


        public bool Exist(string deviceNO)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.DeviceInfo.Count(a => a.DeviceNO == deviceNO) > 0;
            }
        }

        public bool Update(string deviceNO, string ip)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                DeviceInfo item = entities.DeviceInfo.FirstOrDefault(a => a.DeviceNO == deviceNO);
                item.IP = ip;
                return entities.SaveChanges() > 0;
            }
        }

        public bool Update(DeviceInfo info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                DeviceInfo item = entities.DeviceInfo.FirstOrDefault(a => a.DeviceNO == info.DeviceNO);
                item.ConnectType = info.ConnectType;
                item.DefaultGateway = info.DefaultGateway;
                item.Desc = info.Desc;
                item.DeviceDeptID = info.DeviceDeptID;
                item.DevicePlaceID = info.DevicePlaceID;
                item.DeviceTypeID = info.DeviceTypeID;
                item.IP = info.IP;
                item.Port = info.Port;
                item.SubnetMask = info.SubnetMask;
                return entities.SaveChanges() > 0;
            }
        }

        public bool Delete(string deviceNO)
        {
            using (EinsteinEntities context = new EinsteinEntities())
            {
                foreach (var item in context.DeviceInfo.Where(a => a.DeviceNO == deviceNO))
                {
                    context.DeleteObject(item);
                }
                return context.SaveChanges() > 0;
            }
        }


    }
}
